/**
 * appRain v 0.1.x
 *
 * LICENSE
 *
 * This source file is subject to the MIT license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.opensource.org/licenses/mit-license.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@apprain.com so we can send you a copy immediately.
 *
 * @copyright  Copyright (c) 2010 appRain, Inc. (http://www.apprain.com)
 * @license    http://www.opensource.org/licenses/mit-license.php MIT license
 *
 * HELP
 *
 * Official Website
 * http://www.apprain.com/
 *
 * Download Link
 * http://www.apprain.com/download
 *
 * Documents Link
 * http ://www.apprain.com/docs
 */
var appcart =
{
	_attributeerr: false,
	_minitem   :1,
	_buttonobj :null,
	_parentobj :null,
    _buttonref :'.add2cart-button',
	_parentref :'.add2cartwrapper',
	_unittref  :'.add2cart-input',
	_productref:'.productid',
	tqtycartref:'#tqtycart',
	_messageref:'cart-msg',
	_chkobtnref:'.checkout-button',
	_chkolink  :'/checkout',
	_delay	   :'slow',
	_l_img     :siteInfo.baseUrl + '/images/loading.gif',
	loading    :function(ptr)
                {
					if(ptr) {
						jQuery(appcart._parentobj).css('background','url(' + appcart._l_img + ') left center no-repeat');
					}
                    else {
						jQuery(appcart._parentobj).css('background','none');
					}
                },
   getUnitTotal:function()
				 {
					var _obj  = jQuery(appcart._parentobj).children(appcart._unittref);
					
					var total = jQuery(_obj).val();

					if(total < appcart._minitem || isNaN(total))
					{
						total = appcart._minitem;
						jQuery(_obj).val(appcart._minitem);
					}

					return total;
				},
	getProdId  :function()
				{
					return  jQuery(appcart._parentobj).children(appcart._productref).val();
				},
	add2cart   :function()
				{
					if(appcart._attributeerr){
						alert('Please complete the attribute selection.');
						return ;
					}

					appcart._buttonobj = this;
					appcart._parentobj = jQuery(this).parent();
					appcart.loading(true);
				
					jQuery.ajax({
						url: siteInfo.baseUrl + "/appstore/add2qcart/" + appcart.getProdId() + "/" + appcart.getUnitTotal() +  "?ajax=true",
						context: document.body,
						type:'POST',
						data:jQuery('#checkattributeforvalue').serialize(),
						success: function(responseTxt){
							var responseObj = eval('(' + responseTxt + ')');
						  	jQuery(appcart.tqtycartref).text(responseObj.totalitem);							
							jQuery(appcart._parentobj).append('<div class="' + appcart._messageref + '">' + responseObj._message + '</div>');							
							appcart.checkoutbtn(responseObj._hastocheckout);
							appcart.clearmsg();
							appcart.loading(false);
						}
					  });
				},
	checkoutbtn:function(F)
				{
					jQuery(appcart._chkobtnref).removeClass('hide');
					jQuery(appcart._chkobtnref).removeClass('show');

					if(F)
					{
						jQuery(appcart._chkobtnref).addClass('show');
					}
					else
					{
						jQuery(appcart._chkobtnref).addClass('hide');
					}
				},
	checkout   :function()
				{
					window.location = siteInfo.baseUrl + appcart._chkolink;
				},
	clearmsg   :function()
				{
					jQuery('.cart-msg').fadeOut(appcart._delay);
				},
    init       :function()
                {
					jQuery(appcart._chkobtnref).click(appcart.checkout);
					jQuery(appcart._buttonref).click(appcart.add2cart);
                }
}

jQuery(document).ready(appcart.init);


jQuery(document).ready(function() {			

	jQuery('#checkattributeforvalue').submit(function(e){
		e.preventDefault();
	});

	jQuery('.attributelblblock').click(function(){
		var obj = this;
		var data = jQuery(this).attr('data');
		var dataObj = eval('(' + data + ')');
		var hiddenBox = '#hiddenbox_'+dataObj.key;
		var prvVal = jQuery(hiddenBox).val();
		if(jQuery("#suggession").length < 1){
			jQuery('body').prepend('<div id="suggession" class="fancybox-skin"></div>');
		}			
		jQuery(hiddenBox).val(dataObj.value);
		jQuery('.error-attribute').html('');
		jQuery('#suggessionbody').html('');					
		jQuery('#suggession').css('display','none');
		appcart._attributeerr = false;
		jQuery.ajax({
			url:jQuery('#checkattributeforvalue').attr('action') + '/' + dataObj.key,
			context:document.body,
			type:jQuery('#checkattributeforvalue').attr('method'),
			data:jQuery('#checkattributeforvalue').serialize(),
			success:function (responseTxt) {
				var responseObj = eval('(' + responseTxt + ')');
				if(responseObj.status=='Success'){	
					jQuery(obj).parent('.attributelistbox').children('label').removeClass('checkedattribute');
					jQuery(obj).addClass('checkedattribute');
					jQuery('.productpricebox').html(responseObj.price);	
					jQuery('.productid').val(responseObj.id);
				}
				else {
					appcart._attributeerr = true;;
					jQuery(obj).parent('.attributelistbox').children('label').removeClass('checkedattribute');
					jQuery(obj).addClass('checkedattribute');
					jQuery('.error-attribute').html('Plesae try other combination.');					
					var p = jQuery(".attributebox");
					var position = p.position();
					jQuery('#suggession').css('display','block');
					jQuery('#suggession').css('right',(position.left)+'px');
					jQuery('#suggession').css('top',(position.top)+'px');
					jQuery('#suggession').html(responseObj.suggession);								
													
				}
			}
		});
	});
});	
		